
#include "JY901.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"
#include "stdio.h"
#include "pid.h"
#include "motor.h"


/**
 * 数据储存
 */
struct STime		stcTime;
struct SAcc 		stcAcc;
struct SGyro 		stcGyro;
struct SAngle 	stcAngle;
struct SMag 		stcMag;
struct SDStatus stcDStatus;
struct SPress 	stcPress;
struct SLonLat 	stcLonLat;
struct SGPSV 		stcGPSV;
struct SQ       stcQ;

uint8_t ACCCALSW[5] = {0XFF,0XAA,0X01,0X01,0X00};//进入加速度校准模式
uint8_t SAVACALSW[5]= {0XFF,0XAA,0X00,0X00,0X00};//保存当前配置

/**
 * 缓冲区
 */
uint8_t ubuf[256];
uint8_t u_buf[256];
unsigned char ucRxBuffer_jy901[250];
static uint8_t ucRxCnt_jy901=0;

void Uart_JY901CallBack(){

    if (ucRxBuffer_jy901[0] != 0x55) {//数据头不对，则重新开始寻找0x55数据头
        ucRxCnt_jy901=0;
    }
    else if(ucRxCnt_jy901 >= 11)
    {
        switch(ucRxBuffer_jy901[1])
        {
            case 0x50:	memcpy(&stcTime, &ucRxBuffer_jy901[2], 8);break;//memcpy为编译器自带的内存拷贝函数，需引用"string.h"，将接收缓冲区的字符拷贝到数据共同体里面，从而实现数据的解析。
            case 0x51:	memcpy(&stcAcc, &ucRxBuffer_jy901[2], 8);break;
            case 0x52:	memcpy(&stcGyro, &ucRxBuffer_jy901[2], 8);break;
            case 0x53:	memcpy(&stcAngle, &ucRxBuffer_jy901[2], 8);break;
            case 0x54:	memcpy(&stcMag, &ucRxBuffer_jy901[2], 8);break;
            case 0x55:	memcpy(&stcDStatus, &ucRxBuffer_jy901[2], 8);break;
            case 0x56:	memcpy(&stcPress, &ucRxBuffer_jy901[2], 8);break;
            case 0x57:	memcpy(&stcLonLat, &ucRxBuffer_jy901[2], 8);break;
            case 0x58:	memcpy(&stcGPSV, &ucRxBuffer_jy901[2], 8);break;
        }
        ucRxCnt_jy901=0;
    }

    HAL_UART_Receive_IT(&huart1, &ucRxBuffer_jy901[ucRxCnt_jy901++], 1);
}

float IMU_GetRoll(){
    return (float)stcAngle.Angle[0]/32768*180;
}

float IMU_GetPitch(){
    return (float)stcAngle.Angle[1]/32768*180;
}

float IMU_GetYaw(){
    return (float)stcAngle.Angle[2]/32768*180;
}

void IMU_Init(){
    HAL_UART_Receive_IT(&huart1, &ucRxBuffer_jy901[ucRxCnt_jy901++], 1);
}